/*
 * DiscoveryAgent.java
 *
 * Created on February 24, 2007, 2:59 PM
 */

package com.bloomingstars.bluepad.desktop.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.bluetooth.DataElement;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;

/**
 *
 * @author toni
 */
public class BluePadDiscoveryListener implements DiscoveryListener
{
    private List<RemoteDevice> remoteDevicesDiscovered = new ArrayList<RemoteDevice>();
    private List<ServiceRecord> servicesRecordsDiscovered = new ArrayList<ServiceRecord>();
    private List transactionIds = new ArrayList();
    private BluePadDiscoverer discoverer;
    
    public BluePadDiscoveryListener(BluePadDiscoverer discoverer)
    {
        this.discoverer = discoverer;
    }
      
    public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass)
    {        
        try
        {
            String deviceName = remoteDevice.getFriendlyName(false);
            System.out.println("deviceDiscovered: " + deviceName);
            
            // Trigger Serial Port Service Discovery
            if (deviceName.equalsIgnoreCase("da2"))
            {
                remoteDevicesDiscovered.add(remoteDevice);
            }            
        } 
        catch (IOException e)
        {
            System.err.println("Problems while getting the friendly name of a device");
            e.printStackTrace();
        }
    }
    
    public void inquiryCompleted(int complete)
    {
        System.out.println("inquiryCompleted!");
        // Notify listening threads
        synchronized (discoverer)
        {
            discoverer.notify();
        }
            
    }
    
    public void servicesDiscovered(int transId, ServiceRecord[] records)
    {     
        System.out.println("servicesDiscovered!");
        for(int i = 0; i < records.length; i++)
        {
            ServiceRecord record = records[i];
            printService(record);
            servicesRecordsDiscovered.add(record);
        }
    }
    
    public void printService(ServiceRecord rec)
    {
        int[] attrIds = rec.getAttributeIDs();        
        System.out.println("URL: " + rec.getConnectionURL(ServiceRecord.AUTHENTICATE_NOENCRYPT, false));
        
        for (int i=0; i<attrIds.length; i++) 
        {
            DataElement el = rec.getAttributeValue(attrIds[i]);
            System.out.println("Attribute " + attrIds[i] + " : " + el.getDataType());
        }
        System.out.println();
        DataElement el = rec.getAttributeValue(256);
        if (el != null && el.getDataType() == 32)
        {
            System.out.println("Service name: " + (String) el.getValue());
        }
    }
        
    
    public void serviceSearchCompleted(int transId, int complete)
    {
        System.out.println("serviceSearchCompleted!");
        if(complete == SERVICE_SEARCH_ERROR)
        {
            System.out.println("\nSERVICE_SEARCH_ERROR\n");
        }
        if(complete == SERVICE_SEARCH_COMPLETED)
        {
            System.out.println("\nSERVICE_SEARCH_COMPLETED\n");
        }
        if(complete == SERVICE_SEARCH_TERMINATED)
        {
            System.out.println("\n SERVICE_SEARCH_TERMINATED\n");
        }
        if(complete == SERVICE_SEARCH_NO_RECORDS)
        {
            System.out.println("\n SERVICE_SEARCH_NO_RECORDS\n");
        }
        if(complete == SERVICE_SEARCH_DEVICE_NOT_REACHABLE)
        {
            System.out.println("\n SERVICE_SEARCH_DEVICE_NOT_REACHABLE\n");
        }
        
         // Notify listening threads
        synchronized (discoverer)
        {
            discoverer.notify();
        }    
    }
    
    public List<RemoteDevice> getRemoteDevicesDiscovered()
    {
        return remoteDevicesDiscovered;
    }
    
    public List<ServiceRecord> getServicesDiscovered()
    {
        return servicesRecordsDiscovered;
    }
}

